import java.util.Scanner;

public class demo01 {
    //小朋友崇拜圈
    public static int max=0;
    public static void main(String[] args) {
        Scanner sc=new Scanner(System.in);
        int n=sc.nextInt();
        int[] arr=new int[n+1];
        for (int i = 1; i <= n; i++) {
            arr[i]=sc.nextInt();
        }
        for (int i = 1; i <= n; i++) {
            int[] num=new int[n+1];
            dfs(arr,num,i,0);
        }
        System.out.println(max);
    }
    public static void dfs(int[] arr,int[] num,int i,int count){
        num[i]=1;
        int n=arr[i];
        count++;
        while (num[n]!=1){
            num[n]=1;
            count++;
            n=arr[n];
        }
        if(arr[i]==arr[n]) {
            max = Math.max(count, max);
        }
    }
}
